{  Statement: http://acmp.ru/?main=task&id_task=58
   Verdict: Accepted
}
uses
    SysUtils, Math;

var
    t, tt, n, m, i, j, cur : longint;
    a : array[0..110, 0..110] of byte;
    ok : boolean;
    
begin
    reset(input,'input.txt');
    rewrite(output,'output.txt');
    read(t);
    for tt := 1 to t do begin
        ok := true;
        read(n, m);
        for i := 1 to n do
            for j := 1 to m do
                read(a[i][j]);
        if (n > 1) and (m > 1) then begin
            dec(n);
            dec(m);
            for i := 1 to n do
                for j := 1 to m do begin
                    cur := a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
                    if (cur = 0) or (cur = 4) then begin
                        ok := false;
                        break;
                    end;
                end;
        end;
        if ok then writeln('YES') else writeln('NO');
    end;
end.
